package chapter16Condition;

/**
 * Created by LinGer on 2019/6/24
 */
public class ConditionTest2 {

    private static BoundedBuffer bb = new BoundedBuffer();

    private static int time = 30;

    public static void main(String[] args) {

        //启动10个写线程，向BoundedBuffer中写数据
        //启动10个读线程，从BoundedBuffer中读数据
        for (int i = 0; i < 10; i++) {
            new PutThread("p"+i, i).start();
            new TakeThread("t"+i).start();
        }
    }

    static class PutThread extends Thread{
        private int num;

        public PutThread(String name, int num){
            super(name);
            this.num = num;
        }

        @Override
        public void run() {
            try {
                Thread.sleep(time);
                bb.put(num);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    static class TakeThread extends Thread{

        public TakeThread(String name){
            super(name);
        }

        @Override
        public void run() {
            try {
                Thread.sleep(time);
                Integer num = (Integer) bb.take();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
